package org.example.qqrobot.controller.user;
import lombok.extern.slf4j.Slf4j;
import org.example.qqrobot.model.Result;
import org.example.qqrobot.model.dto.ProductQueryDTO;
import org.example.qqrobot.model.dto.UserInfoDTO;
import org.example.qqrobot.model.pojo.User;
import org.example.qqrobot.model.dto.UserLoginDTO;
import org.example.qqrobot.model.vo.InvitationMainVO;
import org.example.qqrobot.model.vo.MessageMainVO;
import org.example.qqrobot.model.vo.UserLoginVO;
import org.example.qqrobot.service.InvitationService;
import org.example.qqrobot.service.MessageService;
import org.example.qqrobot.service.ProductService;
import org.example.qqrobot.service.UserService;
import org.example.qqrobot.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private InvitationService invitationService;



    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) throws Exception{
        log.info("微信用户登录，{}",userLoginDTO.getCode());

        //微信登录
        User user = userService.wxLogin(userLoginDTO);

        Map claims = new HashMap<>();
        claims.put("userId",user.getId());

        //为微信用户生成令牌
        String token = JwtUtils.generateJwt(claims);

        UserLoginVO userLoginVO = new UserLoginVO(user.getId(),user.getOpenid(),token);

        return Result.success(userLoginVO);

    }

    @GetMapping("/getList")
    public Result getList(Long id){
        log.info("获取数据.{}",id);

        User user = userService.getMyUser(id);

        return Result.success(user);

    }

    @PutMapping("/update")
    public Result update(@RequestBody UserInfoDTO userInfoDTO){
        log.info("更新用户的数据，{}",userInfoDTO);
        userService.update(userInfoDTO);
        return Result.success();
    }

    @PostMapping("/lookCollection")
    public Result lookCollection(){
        log.info("打开收藏页面");

        return Result.success(productService.getTheCollection());
    }

    @PostMapping("/lookMyProduct")
    public Result lookMyProduct(){
        log.info("打开我的商品");

        return Result.success(productService.getMyProduct());
    }

    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestParam("message") String message,
                              @RequestParam("id") Long id){
        log.info("发送消息，{}，{}",message,id);
        productService.sendMessage(message,id);

        return Result.success(1);
    }

    @PostMapping("/lookMyMessage")
    public Result lookMyMessage(){
        log.info("查看消息");

        List<MessageMainVO> mList = messageService.lookMyMessage();
        return Result.success(mList);

    }

    @PostMapping("/lookMyInvitation")
    public Result lookMyInvitation(){
        log.info("查看我的帖子");
        List<InvitationMainVO> iList = invitationService.lookMyInvitation();
        return Result.success(iList);
    }






}
